<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BoContabilidad
 *
 * @author EADeveloper
 */
class BoContabilidad extends easoftwarebussinesobject {
    
    /**
     *
     * @param type $name
     * @return \name 
     */
    public static function getMe($name = "BoContabilidad") {
        return easoftwarebussinesobject::getMe($name);
    }

    /**
     * Retorna la informacion normal de un ajuste
     * @param type $id
     * @param type $display
     * @return type 
     */
    public function getAjusteInfo($id) {
        $sql = sprintf("select a.id,a.descripcion,a.monto montoajuste,a.fecha, c.cuenta,c.debe,c.haber,c.monto 
                from cllg_contab_ajuste a, cllg_contab_cuenta_ajuste c
                where c.ajuste=a.id and a.id=%s", $id);
        $ajuste = $this->getArrayData($sql);
        $h = $ajuste[0];
        ob_start();
        ?>
        <div class="dttb">
            <table>
                <thead>
                    <tr>
                        <th>Id Ajuste</th>
                        <th>Fecha</th>
                        <th>Descripcion</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td><?php echo $h["id"]; ?></td>
                        <td><?php echo $h["fecha"]; ?></td>
                        <td><?php echo $h["descripcion"]; ?></td>
                    </tr>   
                </tbody>
            </table> &nbsp;
            <table>
                <thead>
                <tr>
                    <th>Cuenta</th>
                    <th>Debe</th>
                    <th>Haber</th>
                </tr>
                </thead>
                <tbody>
                    <?php foreach ($ajuste as $r) { ?>
                        <tr>
                            <td><?php echo $r["cuenta"] ?></td>
                            <td><?php echo $r["debe"] == 1 ? $r["monto"] : "-" ?></td>
                            <td><?php echo $r["haber"] == 1 ? $r["monto"] : "-" ?></td>
                        </tr>
                        <?php
                    }
                    $r = $ajuste[0];
                    ?>
                </tbody>
                <tfoot>
                    <tr>
                        <td>Total</td>
                        <td><?php echo $r["montoajuste"]; ?></td>
                        <td><?php echo $r["montoajuste"]; ?></td>
                    </tr>
                </tfoot>
            </table>
        </div>
        <?php
        $ajuste["html"] = ob_get_clean();
        return $ajuste;
    }

    /**
     * retorna la informacion invertidad e una juste
     * @param type $id
     * @param type $display
     * @return type 
     */
    public function getAjusteInvertido($id, $display = true) {
        $sql = sprintf("select a.id,a.monto montoajuste,a.fecha, 
                c.cuenta,c.debe HABER,c.haber DEBE,c.monto 
                from cllg_contab_ajuste a, cllg_contab_cuenta_ajuste c
                where c.ajuste=a.id and a.id=%s", $id);
        $ajuste = $this->getArrayData($sql);
        if ($display) {
            ?>
            <div class="dttb">
                <table>
                    <tr>
                        <th>Cuenta</th>
                        <th>Debe</th>
                        <th>Haber</th>
                    </tr>
                    <tbody>
                        <?php foreach ($ajuste as $r) { ?>
                            <tr>
                                <td><?php echo $r["cuenta"] ?></td>
                                <td><?php echo $r["DEBE"] == 1 ? $r["monto"] : "-" ?></td>
                                <td><?php echo $r["HABER"] == 1 ? $r["monto"] : "-" ?></td>
                            </tr>
                            <?php
                        }
                        $r = $ajuste[0];
                        ?>
                    </tbody>
                    <tfoot>
                        <tr>
                            <td>Total</td>
                            <td><?php echo $r["montoajuste"]; ?></td>
                            <td><?php echo $r["montoajuste"]; ?></td>
                        </tr>
                    </tfoot>
                </table>
            </div>
            <?php
        }
        return $ajuste;
    }

    /**
     *
     * @return type 
     */
    public function getLastAjuste() {
        $sql = "select * from cllg_contab_ajuste order by id DESC";
        $r = $this->getArrayData($sql);
        
        return $r[0];
    }

    /**
     *
     * @param type $info
     * @return type 
     */
    public function guardarAjuste($info) {
        $cicloActivo = $this->getCurrentCiclo();
        $ciclo = isset($info[0]["ciclo"]) ? $info[0]["ciclo"] : $cicloActivo["codigo"];

        $sql = sprintf("insert into cllg_contab_ajuste(fecha,monto,ciclo,descripcion) values(%s,%s,%s,%s)"
                , $info[0]["fecha"], $info[0]["monto"], $ciclo, $info[0]["desc"]);
        $u = $this->getLastAjuste();        
        $s = $u["id"] + 1;
        $info = $info[1];
        foreach ($info as $v) {
            $sql = sprintf("insert into cllg_contab_cuenta_ajuste(cuenta,debe,haber,monto,ajuste)
                  values(%s,%s,%s,%s,%s)", $v["cta"], $v["d"], $v["h"], $v["valor"], $s);
        }
        return $s;
    }

    /**
     *
     * @return type 
     */
    public function getCurrentCiclo() {
        $sql = "select * from cllg_contab_ciclo where activo=1";
        $r = $this->getArrayData($sql);
        return $r[0];
    }

}
?>
